﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.ComponentModel.DataAnnotations;
using RM.WebClient.RMServices;
using System.IO;
using System.Windows.Data;

namespace RM.WebClient
{
    public partial class CandidateDetail : UserControl
    {
        byte[] content = null;
        const string CopyandPaste = "Copy and paste your resume here";
        public CandidateDetail()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(CandidateDetail_Loaded);
        }

        private void CandidateDetail_Loaded(object sender, RoutedEventArgs e)
        {
            // btnSave.Click += new RoutedEventHandler(btnSave_Click);
        }


        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            clearFormErrors();
            clearFormData();
        }

        private bool validateCandidateDetails()
        {
            bool isValid = true;
            bool b = false;
            isValid = txtFirstName.validateField("First Name") && isValid;
            isValid = txtLastName.validateField("Last Name") && isValid;
            isValid = dtPickerDOB.validateField("Date Of Birth") && isValid;
            isValid = txtEmailAddress.validateField("Email Address") && isValid;
            if (!string.IsNullOrEmpty(txtEmailAddress.Text))
            {
                isValid = txtEmailAddress.IsEmailValid() && isValid;
            }

            isValid = cbxCountry.validateField("Country") && isValid;
            isValid = cbxCurrentLocation.validateField("Current Location") && isValid;
            isValid = ValidatePhoneNumbers() && isValid;
            isValid = cbxExperianceYears.validateField("Experiance Years") && isValid;
            isValid = cbxExperianceMonths.validateField("Experiance Months") && isValid;
            isValid = cbxSalaryInLacs.validateField("Annual Salary in Lacs") && isValid;
            isValid = cbxSalaryInThousands.validateField("Annual Salary in Thousands") && isValid;
            isValid = cbxFunctionalArea.validateField("Functional Area") && isValid;
            return isValid;
        }

        private bool ValidatePhoneNumbers()
        {
            txtMobileNumber.ClearValidationError();
            txtPhoneNumber.ClearValidationError();
            txtCountryCode.ClearValidationError();
            txtAreaCode.ClearValidationError();
            bool b = true;
            if (string.IsNullOrEmpty(txtMobileNumber.Text) && string.IsNullOrEmpty(txtPhoneNumber.Text))
            {
                txtMobileNumber.SetValidation("Please enter any one of the contact numbers");
                txtMobileNumber.RaiseValidationError();
                txtPhoneNumber.SetValidation("Please enter any one of the contact numbers");
                txtPhoneNumber.RaiseValidationError();
                b = false;
            }
            if (string.IsNullOrEmpty(txtMobileNumber.Text))
            {
                b = txtCountryCode.validateField("Country Code") && b;
                if (!string.IsNullOrEmpty(txtCountryCode.Text))
                {
                    b = txtCountryCode.IsNumberValid() && b;
                }

                b = txtAreaCode.validateField("Area Code") && b;
                if (!string.IsNullOrEmpty(txtAreaCode.Text))
                {
                    b = txtAreaCode.IsNumberValid() && b;
                }

            }
            if (!string.IsNullOrEmpty(txtMobileNumber.Text))
            {
                b = txtMobileNumber.IsNumberValid() && b;
                b = txtMobileNumber.ValidateFixLength(10) && b;
            }

            if (!string.IsNullOrEmpty(txtPhoneNumber.Text))
            {
                b = txtPhoneNumber.IsNumberValid() && b;
            }
            return b;
        }



        private void GetCurrency(Candidate candidate)
        {
            //    bool bINR = (bool)rbtnINR.IsChecked;
            //    bool bUSD = (bool)rbtnUSD.IsChecked;
            //    if (bINR == true)
            //    {
            //        candidate.Currency = rbtnINR.Content.ToString();
            //    }

            //    else if (bUSD == true)
            //    {
            //        candidate.Currency = rbtnUSD.Content.ToString();
            //    }            
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Doc Files (.doc)|*.doc|All Files (*.*)|*.*";
            if (dlg.ShowDialog() == true)
            {
                //Give Security exception
                // txtBrowseResume.Text = dlg.File.FullName;
                using (FileStream stream = dlg.File.OpenRead())
                {
                    // Store file content in 'text' variable
                    //byte[] content = null;
                    if (stream != null)
                    {
                        content = new byte[stream.Length];
                        stream.Read(content, 0, (int)stream.Length - 1);
                        //candidate.SolutionFile = content;
                    }
                    if (content.Length > 0)
                    {
                        txtBrowseResume.Text = "Attachment Uploaded";
                    }
                }
            }

        }

        private void ClearDefaultContents(object sender, RoutedEventArgs e)
        {
            string sDefaultContent = txtCopyResume.Text;
            if (sDefaultContent.Equals(CopyandPaste))
            {
                txtCopyResume.Text = string.Empty;
                txtCopyResume.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            }
        }

        private void SetDefaultContents(object sender, RoutedEventArgs e)
        {
            string sDefaultContent = txtCopyResume.Text;
            if (string.IsNullOrEmpty(sDefaultContent.Trim()))
            {
                txtCopyResume.Text = CopyandPaste;
                txtCopyResume.Foreground = new SolidColorBrush(Color.FromArgb(255, 97, 92, 92));
            }
        }

        private void clearFormData()
        {
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            dtPickerDOB.Text = string.Empty;
            cbxSex.SelectedIndex = 0;
            txtEmailAddress.Text = string.Empty;
            cbxCountry.SelectedIndex = -1;
            cbxCurrentLocation.SelectedIndex = -1;
            txtMobileNumber.Text = string.Empty;
            txtCountryCode.Text = string.Empty;
            txtAreaCode.Text = string.Empty;
            txtPhoneNumber.Text = string.Empty;
            cbxExperianceYears.SelectedIndex = -1;
            cbxExperianceMonths.SelectedIndex = -1;
            cbxSalaryInLacs.SelectedIndex = -1;
            cbxSalaryInThousands.SelectedIndex = -1;
            rbtnINR.IsChecked = true;
            rbtnUSD.IsChecked = false;
            cbxFunctionalArea.SelectedIndex = -1;
            txtBrowseResume.Text = string.Empty;

            txtCopyResume.Text = CopyandPaste;
            txtCopyResume.Foreground = new SolidColorBrush(Color.FromArgb(255, 97, 92, 92));
            //txtCopyResume.Text = string.Empty;
        }

        private void clearFormErrors()
        {
            txtFirstName.ClearValidationError();
            txtLastName.ClearValidationError();
            dtPickerDOB.ClearValidationError();
            txtEmailAddress.ClearValidationError();
            cbxCountry.ClearValidationError();
            cbxCurrentLocation.ClearValidationError();
            txtMobileNumber.ClearValidationError();
            txtCountryCode.ClearValidationError();
            txtAreaCode.ClearValidationError();
            txtPhoneNumber.ClearValidationError();
            cbxExperianceYears.ClearValidationError();
            cbxExperianceMonths.ClearValidationError();
            cbxSalaryInLacs.ClearValidationError();
            cbxSalaryInThousands.ClearValidationError();
            cbxFunctionalArea.ClearValidationError();
        }

        private void linkCandidatesList_Click(object sender, RoutedEventArgs e)
        {
            canvasBottom.Children.Clear();
            canvasBottom.Children.Add(new CandidateList());
        }

        private void txtFirstName_LostFocus(object sender, RoutedEventArgs e)
        {
            txtFirstName.validateField("First Name");
        }

        private void txtLastName_LostFocus(object sender, RoutedEventArgs e)
        {
            txtLastName.validateField("Last Name");
        }

        private void dtPickerDOB_LostFocus(object sender, RoutedEventArgs e)
        {
            dtPickerDOB.validateField("Date Of Birth");
        }

        private void txtEmailAddress_LostFocus(object sender, RoutedEventArgs e)
        {
            txtEmailAddress.validateField("Email Address");
            if (!string.IsNullOrEmpty(txtEmailAddress.Text))
            {
                txtEmailAddress.IsEmailValid();
            }
        }

        private void cbxCountry_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxCountry.validateField("Country");
        }

        private void cbxCurrentLocation_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxCurrentLocation.validateField("Current Location");
        }

        private void txtMobileNumber_LostFocus(object sender, RoutedEventArgs e)
        {
            bool b = false;
            //txtMobileNumber.validateField("Mobile Number");
            if (!string.IsNullOrEmpty(txtMobileNumber.Text))
            {
                b = txtMobileNumber.IsNumberValid();
            }

            if (b)
            {
                txtMobileNumber.ValidateFixLength(10);
            }
        }

        private void txtCountryCode_LostFocus(object sender, RoutedEventArgs e)
        {
            //ValidatePhoneNumbers();
        }

        private void txtAreaCode_LostFocus(object sender, RoutedEventArgs e)
        {
            //ValidatePhoneNumbers();
        }

        private void txtPhoneNumber_LostFocus(object sender, RoutedEventArgs e)
        {
            ValidatePhoneNumbers();
        }

        private void cbxExperianceYears_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxExperianceYears.validateField("Experiance Years");
        }

        private void cbxExperianceMonths_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxExperianceMonths.validateField("Experiance Months");
        }

        private void cbxSalaryInLacs_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxSalaryInLacs.validateField("Annual Salary in Lacs");
        }

        private void cbxSalaryInThousands_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxSalaryInThousands.validateField("Annual Salary in Thousands");
        }

        private void cbxFunctionalArea_LostFocus(object sender, RoutedEventArgs e)
        {
            cbxFunctionalArea.validateField("Functional Area");
        }

        private void imgCandidatesList_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            canvasBottom.Children.Clear();
            canvasBottom.Children.Add(new CandidateList());
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (validateCandidateDetails() == true)
            {
                btnSave.IsEnabled = false;
                Candidate candidate = new Candidate();

                candidate.FirstName = txtFirstName.Text;
                candidate.LastName = txtLastName.Text;
                candidate.FullName = candidate.FirstName + " " + candidate.LastName;
                candidate.DoB = dtPickerDOB.DisplayDate.Date;
                candidate.Sex = ((ContentControl)cbxSex.SelectedItem).Content.ToString();
                candidate.EmailAddress = txtEmailAddress.Text;
                candidate.CurrentLocation = ((ContentControl)cbxCurrentLocation.SelectedItem).Content.ToString();
                candidate.Country = ((ContentControl)cbxCountry.SelectedItem).Content.ToString();
                candidate.MobileNo = txtMobileNumber.Text;
                candidate.CountryCode = txtCountryCode.Text;
                candidate.AreaCode = txtAreaCode.Text;
                candidate.LandLineNo = txtPhoneNumber.Text;
                candidate.TotalExpYr = ((ContentControl)cbxExperianceYears.SelectedItem).Content.ToString();
                candidate.TotalExpMonth = ((ContentControl)cbxExperianceMonths.SelectedItem).Content.ToString();
                candidate.CurrAnualSalaryLac = ((ContentControl)cbxSalaryInLacs.SelectedItem).Content.ToString();
                candidate.CurrAnualSalaryThsnd = ((ContentControl)cbxSalaryInThousands.SelectedItem).Content.ToString();
                candidate.Currency = rbtnINR.Content.ToString();
                //if (txtCopyResume.Text.Trim() != CopyandPaste)
                //    content = txtCopyResume.Text;
                //candidate.Resume = HttpUtility.HtmlDecode(content);
                //GetCurrency(candidate);
                candidate.Resume = content;
                candidate.FuncArea = ((ContentControl)cbxFunctionalArea.SelectedItem).Content.ToString();
                candidate.CreatedDate = DateTime.Now;
                candidate.ModifiedDate = DateTime.Now;

                //if (string.IsNullOrEmpty(txtFirstName.Text))
                //    txtFirstName.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 10, 26, 28));

                CandidateServiceClient candidateServices = new CandidateServiceClient();
                candidateServices.Candidate_SaveCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(candidateServices_Candidate_SaveCompleted);
                candidateServices.Candidate_SaveAsync(candidate);
                btnSave.IsEnabled = true;
                canvasBottom.Children.Clear();
                canvasBottom.Children.Add(new CandidateList());
            }
        }

        void candidateServices_Candidate_SaveCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageDialog.Show(Utilities.Constants.MsgCandidateSaveSuccess);
        }
    }
}
